﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using BarCode;
using System.Windows.Forms;

namespace Handrise.CDCable.Mobile.Business
{
    /// <summary>
    /// 扫描服务
    /// </summary>
    public class ScanService
    {
        private Barcode barcode; //条码设备

        public ScanService(Form form)
        {
            try
            {
                barcode = new Barcode(form);
                barcode.EnableScanner = true;
                barcode.EnableBeep = true;
                barcode.OnRead += new Barcode.ScannerReadEventHandler(barcode_OnRead);
            }
            catch
            {
            }
        }

        public event ScanReadHandler OnReadCode;

        void barcode_OnRead(object sender, ReaderData readerData)
        {
            try
            {
                if (OnReadCode != null)
                    OnReadCode(readerData.Text);

            }
            catch (Exception)
            {
            }
        }

        /// <summary>
        /// 开始扫描
        /// </summary>
        /// <returns></returns>
        public void Scan()
        {
            try
            {
                barcode.ScanBarcode();
            }
            catch (Exception)
            {
            }
        }

        public void Close()
        {
            try
            {
                barcode.Disconnect();
            }
            catch (Exception)
            {
            } 
        }
    }

    public delegate void ScanReadHandler(string code);
}
